package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.jaina.dto.projectextensionrelation.ProjectExtensionRelationDTO;
import com.guldan.mapper.db.entity.*;
import com.guldan.mapper.db.mapper.ProjectExtensionRelationMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectcategory.ProjectCategorySearchDTO;
import com.guldan.jaina.dto.projectextensiondic.ProjectExtensionDicDTO;
import com.guldan.jaina.dto.projectextensiondic.ProjectExtensionDicSearchDTO;
import com.guldan.jaina.utils.ProjectExtensionDicDomainConvertUtils;
import com.guldan.mapper.db.mapper.ProjectExtensionDicMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectExtensionDicManager extends BaseManager {

    @Autowired
    private ProjectExtensionDicMapperExt projectExtensionDicMapperExt;
    @Autowired
    private ProjectExtensionRelationManager projectExtensionRelationManager;

    //获取单个项目扩展属性信息
    public ProjectExtensionDicDTO get(Integer projectExtensionDicId) {
        return ProjectExtensionDicDomainConvertUtils.convert(projectExtensionDicMapperExt.selectByPrimaryKey(projectExtensionDicId));
    }

    //新增/更新单个项目扩展属性，传入：ProjectExtensionDicDTO
    public ProjectExtensionDicDTO save(ProjectExtensionDicDTO dto) {
        ProjectExtensionDic entity;
        if (dto.getExtId() != null && dto.getExtId() > 0) {
            entity = projectExtensionDicMapperExt.selectByPrimaryKey(dto.getExtId());
            entity.setExtName(dto.getExtName());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectExtensionDicMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectExtensionDic();
            entity.setExtName(dto.getExtName());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectExtensionDicMapperExt.insertSelective(entity);
        }
        dto = ProjectExtensionDicDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目扩展属性信息
    public Boolean delete(Integer projectExtensionDicId) {
        ProjectExtensionDic entity = projectExtensionDicMapperExt.selectByPrimaryKey(projectExtensionDicId);
        entity.setIsDelete(true);
        return projectExtensionDicMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //不分页全部查询项目扩展属性信息列表
    public List<ProjectExtensionDicDTO> all(ProjectExtensionDicSearchDTO projectExtensionDicSearchDTO) {
        ProjectExtensionDicExample example = buildSearchExample(projectExtensionDicSearchDTO);
        List<ProjectExtensionDicDTO> data = new ArrayList<ProjectExtensionDicDTO>();
        List<ProjectExtensionDic> list = projectExtensionDicMapperExt.selectByExample(example);
        for (ProjectExtensionDic entity : list) {
            ProjectExtensionDicDTO dto = ProjectExtensionDicDomainConvertUtils.convert(entity);
            data.add(dto);
        }
        return data;
    }

    //build查询Example
    private ProjectExtensionDicExample buildSearchExample(ProjectExtensionDicSearchDTO search) {
        ProjectExtensionDicExample example = new ProjectExtensionDicExample();
        ProjectExtensionDicExample.Criteria criteria = example.createCriteria();

        if (search.getExtId() != null && search.getExtId() > 0) {
            criteria.andExtIdEqualTo(search.getExtId());
        }
        if (StringUtils.isNotBlank(search.getExtName())) {
            criteria.andExtNameEqualTo(search.getExtName());
        }

        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("ext_id asc");
        }
        return example;
    }

    //获取某个项目的所有扩展属性列表
    public List<ProjectExtensionDicDTO> listByProjectId(Integer projectId) {
        List<ProjectExtensionRelation> relationlist = projectExtensionRelationManager.getProjectExtensionRelationListByProjectId(projectId);
        List<Integer> ids = new ArrayList<>();
        List<ProjectExtensionDicDTO> dicDtoList = new ArrayList<>();
        if (relationlist != null && relationlist.size() > 0) {
            for (ProjectExtensionRelation projectExtensionRelation : relationlist) {
                ids.add(projectExtensionRelation.getExtId());
            }
            ProjectExtensionDicExample extensionDicExample = new ProjectExtensionDicExample();
            ProjectExtensionDicExample.Criteria extensionDicCriteria = extensionDicExample.createCriteria();
            extensionDicCriteria.andExtIdIn(ids);
            extensionDicCriteria.andIsDeleteEqualTo(false);
            List<ProjectExtensionDic> dicList = projectExtensionDicMapperExt.selectByExample(extensionDicExample);
            for (ProjectExtensionDic entity : dicList) {
                dicDtoList.add(ProjectExtensionDicDomainConvertUtils.convert(entity));
            }
        }
        return dicDtoList;
    }

}
